fix(tracer): gate SDK behind env flag, handle errors gracefully, add shutdown hook - #59
Merged
Merged
Conversation
…shutdown hook
- Add isTracingEnabled() that checks TRACING_ENABLED=true/1 or any
non-empty OTLP_ENDPOINT; tracing is off by default
- Wrap sdk.start() in try/catch: connection/config errors are logged
as warnings and never crash or spam the process
- Register SIGTERM and SIGINT handlers that call sdk.shutdown() so
spans flush cleanly on process exit
- Expose initTracing(env) for testability; auto-called at import time
- Export isTracingEnabled() for direct unit testing of gate logic
- Add 22 tests covering: disabled default, all enabled paths (true/1/
OTLP_ENDPOINT), error handling (no throw, warn logged), withSpan
lifecycle (success, error status, span always ends)
- Document TRACING_ENABLED, OTLP_ENDPOINT, SERVICE_NAME in README and
.env.example; add Jaeger quick-start example
- Wire tests/tracer.test.ts into the npm test command
Fixes: tracing started unconditionally at import; SIGINT not handled;
no env gate; connection errors could spam the process
Closes #<tracer-env-gate-issue>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
src/tracer.tscalledsdk.start()unconditionally at import time withOTLP_ENDPOINTdefaulting tohttp://localhost:4318. Every run of the service started tracing and attempted to export spans to a local collector , even in development or CI where no collector exists , producing connection errors, overhead, and no way to disable it. There was also no SIGINT handler and no graceful shutdown.Changes
src/tracer.tsisTracingEnabled(env):pure gate function; returnstrueonly whenTRACING_ENABLED=true/1orOTLP_ENDPOINTis set (non-empty). Off by default.initTracing(env): wrapsNodeSDKconstruction andsdk.start()in atry/catch; any startup error is logged as awarnand the process continues. Has a duplicate-call guard so it's safe to call multiple times.initTracing()at import time : no change to existing callers.SIGTERMandSIGINTshutdown hooks (SIGINTwas missing before).isTracingEnabledandinitTracingfor direct unit testing.tests/tracer.test.ts(new, 22 tests)isTracingEnabled(): all env combinations , absent, unrecognised,true,1, endpoint-only, empty endpointinitTracing(): disabled path (NodeSDK never called), enabled path (SDK starts), error handling (no throw onstart()failure, warning logged)withSpan(): success path, ERROR status set on throw, span always ends infinallypackage.jsontests/tracer.test.tsto thenpm testcommand so it runs in CI..env.exampleTRACING_ENABLED,OTLP_ENDPOINT,SERVICE_NAME.README.mdAcceptance criteria
TRACING_ENABLED=true/1orOTLP_ENDPOINTis setSIGTERMandSIGINTTRACING_ENABLED,OTLP_ENDPOINT,SERVICE_NAMEdocumented in README and.env.examplenpm run build✅,npm run lint0 errors ✅,npm test209/209 ✅)##Closes
closes #43